import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { GlobalSettingService } from '../services/global-setting.service';

@Injectable()
export class BookService {

	constructor(private http: HttpClient, private globalSetting: GlobalSettingService) { }

	public getList(option?: any): any {
		return this.http.get(this.globalSetting.getLibraryWebApiRootUrl() + 'book/list', {
			params: option
		});
	}

	public getSingle(bookid: string): any {
		return this.http.get(this.globalSetting.getLibraryWebApiRootUrl() + 'book/single', {
			params: { id: bookid }
		});
	}

	public update(book: any): any {
		let formData = new FormData();
		formData.append('id', book.id);
		formData.append('isbn', book.isbn);
		formData.append('name', book.name);
		formData.append('publishDate', book.publishDate);
		formData.append('categoryId', book.categoryId);
		formData.append('publisherId', book.publisherId);
		formData.append('authorId', book.authorId);
		formData.append('introduce', book.introduce);
		formData.append('image', book.image);

		return this.http.post(this.globalSetting.getLibraryWebApiRootUrl() + 'book/update', formData);
	}

	public create(book: any): any {
		let formData = new FormData();
		formData.append('isbn', book.isbn);
		formData.append('name', book.name);
		formData.append('publishDate', book.publishDate);
		formData.append('categoryId', book.categoryId);
		formData.append('publisherId', book.publisherId);
		formData.append('authorId', book.authorId);
		formData.append('introduce', book.introduce);
		formData.append('image', book.image);

		return this.http.post(this.globalSetting.getLibraryWebApiRootUrl() + 'book/create', formData);
	}

	public putaway(book: any): any {
		return this.http.post(this.globalSetting.getLibraryWebApiRootUrl() + 'book/putaway', {
			bookId: book.bookId, count: book.count
		});
	}

	public getBooksBySection(id: string): any {
		return this.http.get(this.globalSetting.getLibraryWebApiRootUrl() + 'book/GetBooksBySection', {
			params: { sectionId: id }
		});
	}

}
